
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _extends2 = _interopRequireDefault(require('@babel/runtime/helpers/extends'));

const _utils = require('../utils');

const _constant = require('../utils/constant');

const _router = require('../utils/router');

const _relation = require('../mixins/relation');

const _info = _interopRequireDefault(require('../info'));

const _icon = _interopRequireDefault(require('../icon'));

// Utils
// Mixins
// Components
const _createNamespace = (0, _utils.createNamespace)('grid-item');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];

const _default = createComponent({
  mixins: [(0, _relation.ChildrenMixin)('vanGrid')],
  props: (0, _extends2.default)({}, _router.routeProps, {
    dot: Boolean,
    text: String,
    icon: String,
    iconPrefix: String,
    info: [Number, String],
    badge: [Number, String],
  }),
  computed: {
    style: function style() {
      const _this$parent = this.parent;
      const { square } = _this$parent;
      const { gutter } = _this$parent;
      const { columnNum } = _this$parent;
      const percent = `${100 / columnNum}%`;
      const style = {
        flexBasis: percent,
      };

      if (square) {
        style.paddingTop = percent;
      } else if (gutter) {
        const gutterValue = (0, _utils.addUnit)(gutter);
        style.paddingRight = gutterValue;

        if (this.index >= columnNum) {
          style.marginTop = gutterValue;
        }
      }

      return style;
    },
    contentStyle: function contentStyle() {
      const _this$parent2 = this.parent;
      const { square } = _this$parent2;
      const { gutter } = _this$parent2;

      if (square && gutter) {
        const gutterValue = (0, _utils.addUnit)(gutter);
        return {
          right: gutterValue,
          bottom: gutterValue,
          height: 'auto',
        };
      }
    },
  },
  methods: {
    onClick: function onClick(event) {
      this.$emit('click', event);
      (0, _router.route)(this.$router, this);
    },
    genIcon: function genIcon() {
      const h = this.$createElement;
      const iconSlot = this.slots('icon');
      const info = (0, _utils.isDef)(this.badge) ? this.badge : this.info;

      if (iconSlot) {
        return h('div', {
          class: bem('icon-wrapper'),
        }, [iconSlot, h(_info.default, {
          attrs: {
            dot: this.dot,
            info,
          },
        })]);
      }

      if (this.icon) {
        return h(_icon.default, {
          attrs: {
            name: this.icon,
            dot: this.dot,
            info,
            size: this.parent.iconSize,
            classPrefix: this.iconPrefix,
          },
          class: bem('icon'),
        });
      }
    },
    getText: function getText() {
      const h = this.$createElement;
      const textSlot = this.slots('text');

      if (textSlot) {
        return textSlot;
      }

      if (this.text) {
        return h('span', {
          class: bem('text'),
        }, [this.text]);
      }
    },
    genContent: function genContent() {
      const slot = this.slots();

      if (slot) {
        return slot;
      }

      return [this.genIcon(), this.getText()];
    },
  },
  render: function render() {
    let _ref;

    const h = arguments[0];
    const _this$parent3 = this.parent;
    const { center } = _this$parent3;
    const { border } = _this$parent3;
    const { square } = _this$parent3;
    const { gutter } = _this$parent3;
    const { clickable } = _this$parent3;
    return h('div', {
      class: [bem({
        square,
      })],
      style: this.style,
    }, [h('div', {
      style: this.contentStyle,
      attrs: {
        role: clickable ? 'button' : null,
        tabindex: clickable ? 0 : null,
      },
      class: [bem('content', {
        center,
        square,
        clickable,
        surround: border && gutter,
      }), (_ref = {}, _ref[_constant.BORDER] = border, _ref)],
      on: {
        click: this.onClick,
      },
    }, [this.genContent()])]);
  },
});

exports.default = _default;
